//
//  MKUpDateViewController.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/23.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKUpDateViewController.h"

@interface MKUpDateViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *updateDayDataArray;
@property (nonatomic, retain) NSMutableArray *updateWeekDataArray;

@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, retain) UITableView *upDateTableView;
@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) UICollectionView *upDateCollectionView;

@property (nonatomic, strong) UIButton *backButton;

@end

@implementation MKUpDateViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _updateDayDataArray = [[NSMutableArray alloc] init];
        _updateWeekDataArray = [[NSMutableArray alloc] init];
        _dataArray = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSString *todayUpdateUrl = @"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/A150E278-24F0-41DE-A2C6-09C22E99B80D/timeline_type/1/page/1/listcnt/20";
//    NSString *weakUpdateUrl = @"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/A150E278-24F0-41DE-A2C6-09C22E99B80D/timeline_type/2/page/1/listcnt/20";
//    NSString *upDatePlanUrl = @"http://mobilev3.ac.qq.com/html5/timelist/wm-timelist.html";
//    [self getDataWithUrl:todayUpdateUrl];
    [self getData];
    
    [self setUpSegmentControl];
    
    [self customNavBar];
}
#pragma mark - CustomNavBar
- (void)customNavBar
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 23, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - GetData
- (void)getData
{
    [AFNETWORK getDataWithNoCacheString:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/A150E278-24F0-41DE-A2C6-09C22E99B80D/timeline_type/1/page/1/listcnt/20" block:^(id block) {
        
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            MKUpDateModel *updateModel = [[MKUpDateModel alloc] initWithDictionary:dic];
            [_updateDayDataArray addObject:updateModel];
        }
        
        [_dataArray addObject:_updateDayDataArray];
        
        [AFNETWORK getDataWithNoCacheString:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/A150E278-24F0-41DE-A2C6-09C22E99B80D/timeline_type/2/page/1/listcnt/20" block:^(id block) {
            
            for (NSDictionary *dic in [block objectForKey:@"data"]) {
                MKUpDateModel *updateModel = [[MKUpDateModel alloc] initWithDictionary:dic];
                [_updateWeekDataArray addObject:updateModel];
            }
            
            [_dataArray addObject:_updateWeekDataArray];
            [self setUpCollectionView];
            
        }];
    }];
    
}
#pragma mark - SetUpCollectionView
- (void)setUpCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 110);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _upDateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, SCREEN_HEIGHT - 110) collectionViewLayout:flowLayout];
    
    _upDateCollectionView.delegate = self;
    _upDateCollectionView.dataSource = self;
    
    _upDateCollectionView.pagingEnabled = YES;
    _upDateCollectionView.scrollEnabled = NO;
    
    [self.view addSubview:_upDateCollectionView];
    
    [_upDateCollectionView registerClass:[MKUpDateCollectionViewCell class] forCellWithReuseIdentifier:@"reuseIdentifier"];
    [_upDateCollectionView registerClass:[MKUpDatePlanCollectionViewCell class] forCellWithReuseIdentifier:@"reuseIdentifierPlan"];
}
#pragma mark - CollectionView DataSource&Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MKUpDateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    MKUpDatePlanCollectionViewCell *planCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdentifierPlan" forIndexPath:indexPath];
    if (indexPath.row < 2) {
        cell.upDateDataArray = [_dataArray objectAtIndex:indexPath.row];
//        [cell setDelegate:self];
        return cell;
    }
    else
    {
        [planCell setUrl:@"http://mobilev3.ac.qq.com/html5/timelist/wm-timelist.html"];
        return planCell;
    }
}

#pragma mark - 创建分段选择器
- (void)setUpSegmentControl
{
    NSArray *array = [NSArray arrayWithObjects:@"今日更新", @"本周更新", @"更新计划", nil];
    _segmentControl = [[UISegmentedControl alloc] initWithItems:array];
    [_segmentControl setFrame:CGRectMake(10, 70, SCREEN_WIDTH - 20, 30)];
//    [_segmentControl setBackgroundColor:[UIColor redColor]];
    [_segmentControl setSelectedSegmentIndex:0];
    [_segmentControl setTintColor:[UIColor colorWithRed:0.9 green:0.32 blue:0.07 alpha:1]];
    [_segmentControl addTarget:self action:@selector(segmentedClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentControl];
}
- (void)segmentedClick:(UISegmentedControl *)sender
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.selectedSegmentIndex inSection:0];
    switch (sender.selectedSegmentIndex) {
        case 0:
        {
//            NSLog(@"每日");
//            _dataArray = _updateDayDataArray;
            [_upDateCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
            [_upDateCollectionView reloadData];
        }
            break;
        case 1:
        {
//            NSLog(@"每周");
//            _dataArray = _updateWeekDataArray;
            [_upDateCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
            [_upDateCollectionView reloadData];
        }
            break;
        case 2:
        {
//            NSLog(@"计划");
            [_upDateCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
            [_upDateCollectionView reloadData];
        }
            break;
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
